package com.hsoft.zhxy.app.xtgl_02_01.service.impl;


import com.hsoft.zhxy.app.common.model.entity.JcggDmx;
import com.hsoft.zhxy.app.xtgl_02_01.mapper.JcggDmflMapper;
import com.hsoft.zhxy.app.common.model.entity.JcggDmfl;
import com.hsoft.zhxy.app.xtgl_02_01.mapper.JcggDmxMapper;
import com.hsoft.zhxy.app.xtgl_02_01.model.vo.JcggDmxVO;
import com.hsoft.zhxy.app.xtgl_02_01.service.JcggDmflService;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.hsoft.zhxy.common.base.BaseService;
import com.hsoft.zhxy.common.result.ResultBody;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

 /**
 * All rights Reserved, Designed By 哈尔滨工业大学软件工程股份有限公司
 * 
 * @Title: 代码分类 Service层接口实现类
 * @Package 
 * @Description: 
 * @author: system
 * @date: 2020-10-26
 */
@Log4j2
@Service
public class JcggDmflServiceImpl extends BaseService implements JcggDmflService {

    @Autowired
    private JcggDmflMapper jcggDmflMapper;

    @Autowired
    private JcggDmxMapper jcggDmxMapper;


    /**
     * 查询单条数据
     * @param jcggDmfl
     * @return
     */
    public JSONObject findOne(JcggDmfl jcggDmfl) {
        JcggDmfl rs = jcggDmflMapper.selectOne(new QueryWrapper<JcggDmfl>().setEntity(jcggDmfl));
        return ResultBody.setData(rs);
    }


    /**
     * 查询列表数据
     * @param jcggDmfl
     * @return
     */
    public JSONObject findList(JcggDmfl jcggDmfl) {
        List<JcggDmfl> list = jcggDmflMapper.selectList(new QueryWrapper<JcggDmfl>().setEntity(jcggDmfl));
        return ResultBody.setData(list);
    }


    /**
     * 查询分页数据
     * @param jcggDmfl
     * @param page
     * @param pageSize
     * @return
     */
    public JSONObject findListByPage(JcggDmfl jcggDmfl, long page, long pageSize) {

        Page<JcggDmfl> pageObj = new Page<JcggDmfl>(page, pageSize);
        QueryWrapper<JcggDmfl> queryWrapper = new QueryWrapper<JcggDmfl>();
        queryWrapper.like(StringUtils.isNotBlank(jcggDmfl.getDmflmc()),"dmflmc",jcggDmfl.getDmflmc())
                .orderByAsc("pxh");
        IPage<JcggDmfl> result = jcggDmflMapper.selectPage(pageObj, queryWrapper);
        return ResultBody.setData(result.getTotal(), result.getRecords());
    }

    public  JSONObject findDmTree(){
        List<JcggDmxVO> list = jcggDmxMapper.findList();
        return  ResultBody.setData(list);
    }

    public  JSONObject findDmChildTree(JcggDmxVO jcggDmx){
        List<JcggDmxVO> list = jcggDmxMapper.findChildList(jcggDmx.getSjdmid());
        return  ResultBody.setData(list);
    }

    /**
     * 新增
     * @param jcggDmfl
     * @return
     */
    public JSONObject add(JcggDmfl jcggDmfl) {
        jcggDmfl.setCjsj(LocalDateTime.now());
        jcggDmfl.setCjr(getYhid());
        jcggDmfl.setBgsj(LocalDateTime.now());
        jcggDmfl.setBgr(getYhid());
        return ResultBody.setData(jcggDmflMapper.insert(jcggDmfl));
    }


    /**
     * 修改
     * @param jcggDmfl
     * @return
     */
    public JSONObject update(JcggDmfl jcggDmfl) {
        jcggDmfl.setBgsj(LocalDateTime.now());
        jcggDmfl.setBgr(getYhid());
        return ResultBody.setData(jcggDmflMapper.updateById(jcggDmfl));
    }


    /**
     * 删除
     * @param jcggDmfl
     * @return
     */
    public JSONObject delete(JcggDmfl jcggDmfl) {
        return ResultBody.setData(jcggDmflMapper.delete(new QueryWrapper<JcggDmfl>().setEntity(jcggDmfl)));
    }


}
